package com.sihan.swvp.service.sys;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sihan.swvp.dao.sys.EnterpriseDao;
import com.sihan.swvp.entity.sys.Enterprise;
import com.sihan.swvp.entity.sys.User;

@Component
public class EnterpriseService {
	@Autowired
	EnterpriseDao enterpriseDao;
	
	
	public List<Map<String, Object>> getEnterprise(String cd,User user) {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list =  enterpriseDao.getEnterprise(cd,user.getDept(),user.getId());
		
		return list;
	}

	
	public Integer addEnterprise(Enterprise enterprise){
		if(null == enterprise.getId()){
			
			enterpriseDao.addEnterprise(enterprise);
			
			if(enterprise.getPid() == 0 && enterprise.getId() != null){
				enterprise.setId(enterprise.getId());
				enterpriseDao.upateEnterprise(enterprise);
			}
		}
		else{
			enterpriseDao.upateEnterprise(enterprise);
		}
		
		return enterprise.getId();
	}


	public int upateEnterprise(Enterprise enterprise) {
		// TODO Auto-generated method stub
		return enterpriseDao.upateEnterprise(enterprise);
	}


	public void deleteEnterprise(Integer node,Long user) {
		// TODO Auto-generated method stub
		
		deleteUserByE(node,user);
	}
	
	private void deleteUserByE(Integer node,Long user){
		enterpriseDao.deleteEnterprise(node,user);
		List<Enterprise> es = enterpriseDao.getEnterpriseByPid(node);
		for (Enterprise enterprise : es) {
			deleteUserByE(enterprise.getId(), user);
		}
	}
	
	public Enterprise getEnterPriseById(long id){
		
		
		return enterpriseDao.getEnterpriseById(id);
	}
	
	
	public int isCompany(String eid,String name){
		return enterpriseDao.isCompany(eid,name);
	}


	public int validDept(String eid, String id, String name) {
		// TODO Auto-generated method stub
		return enterpriseDao.validDept(eid,id,name);
	}


	public List<Map<String, Object>> getPrivEnterPrise(User user) {
		// TODO Auto-generated method stub
		return enterpriseDao.getPrivEnterPrise(user);
	}
	
	public int getNameValidate(Map<String, Object> param){
		return enterpriseDao.getNameValidate(param);
	}
	
}
